package cn.xunjie.ymcc.web.controller;

import cn.xunjie.ymcc.domain.PayOrder;
import cn.xunjie.ymcc.dto.AlipayNotifyDto;
import cn.xunjie.ymcc.dto.PayApplyDto;
import cn.xunjie.ymcc.result.JSONResult;
import cn.xunjie.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.factory.Factory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController {
    @Autowired
    private IPayOrderService payOrderService;
    @GetMapping("/checkPayOrder/{orderNo}")
    public JSONResult checkPayOrder(@PathVariable("orderNo") String orderNo){
        PayOrder payOrder = payOrderService.selectOrderNo(orderNo);
        return payOrder == null?JSONResult.error():JSONResult.success();
    }
    @PostMapping("/apply")
    public JSONResult apply(@RequestBody PayApplyDto dto){
        String formStr = payOrderService.apply(dto);//向支付宝发起申请生成form表单
        return JSONResult.success(formStr);
    }

    @RequestMapping("/apply/notify")
    public String notify(AlipayNotifyDto dto){
        try {
           return payOrderService.notify(dto);

        } catch (Exception e) {
            e.printStackTrace();
            return "Fail";
        }

    }

}
